package org.dicker.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.dicker.dao.TypeDao;
import org.dicker.entity.MessageInfo;
import org.dicker.entity.TypeInfo;
import org.dicker.util.ORMUtil;

public class TypeDaoImpl implements TypeDao {
	
	/**全部*/
	public List<TypeInfo> selectAll(){
		String sql="SELECT * from tb_type";
		return ORMUtil.getInstance().selectList(TypeInfo.class, sql);
	}
	
	@Override
	public int selectCount(String name) {
		String sql="SELECT count(*) from tb_type" 
				+ " where 1=1";
		List<Object> valueList = new ArrayList<Object>();
		
		if(name!=null && !name.equals("")){
			sql+=" and type_name like ?";
			String wenhao="%"+name+"%";
			valueList.add(wenhao);
		}
		long value= (long) ORMUtil.getInstance().selectSingleValue("count(*)", sql, valueList.toArray());
		return (int) value;
	}

	
	@Override
	public List<TypeInfo> selectByList(String name, int page, int pageSize) {
		String sql="SELECT * from tb_type"
				+ " where 1=1";
		List<Object> valueList = new ArrayList<Object>();
		
		if(name!=null && !name.equals("")){
			sql+=" and type_name like ?";
			String wenhao="%"+name+"%";
			valueList.add(wenhao);
		}
		
		
		if(page!=-1 && pageSize!=-1){
			sql+=" ORDER BY type_id LIMIT ?,?";
			valueList.add((page-1)*pageSize);
			valueList.add(pageSize);
		}
		
		
		
		return ORMUtil.getInstance().selectList(TypeInfo.class, sql, valueList.toArray());
		
	}


	@Override
	public int deleteById(int typeid) {
		String sql="DELETE FROM tb_type WHERE type_id=?";
		return ORMUtil.getInstance().executeDML(sql, typeid);
	}


	@Override
	public boolean updateById(TypeInfo tinfo) {
		String sql="update tb_type set type_type=?,type_name=?,icon_url=?"
					+" where type_id=?";
		return ORMUtil.getInstance().executeDML(sql,tinfo.getType_type(),tinfo.getType_name(),tinfo.getIcon_url(),tinfo.getType_id())>0;
	}


	@Override
	public TypeInfo selectByInfo(int id) {
		String sql="SELECT * FROM tb_type where type_id=?";
		
		List<TypeInfo> tlList = ORMUtil.getInstance().selectList(TypeInfo.class, sql, id);
		return tlList.size()>0? tlList.get(0):null;
	}


	@Override
	public boolean addType(TypeInfo tInfo) {
		String sql="INSERT INTO tb_type(type_type,type_name,icon_url) "
					+ " VALUES (?,?,?)";
		return ORMUtil.getInstance().executeDML(sql, tInfo.getType_type(),tInfo.getType_name(),tInfo.getIcon_url())>0;
	}

	

	
	
	
	
}
